class Solution {
public:
    int check(TreeNode* root){
        if(!root){
            return -1;
        }
        int l = check(root->left);
        int r = check(root->right);
        
        if(l == -2 || r == -2){
            return -2;
        }
        
        if((l == -1 || l == root->val) && (r == -1 || r == root->val)){
            return root->val;
        }
        return -2;
    }
                                        
    bool isUnivalTree(TreeNode* root) {
        return check(root) == -2 ? false : true;
    }
};
